package com.yuexun.dhdoctor.module.my.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.my.model.impl.ResetPasswordModel;
import com.yuexun.dhdoctor.module.my.presenter.impl.ResetPasswordPresenter;
import com.yuexun.dhdoctor.module.my.ui.view.IResetPasswordView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/19 0019.
 */

public class Act_Change_Psw extends BaseKJActivity implements IResetPasswordView {

    @Bind(R.id.oldPsw)
    EditText oldPsw;
    @Bind(R.id.newPsw)
    EditText newPsw;
    @Bind(R.id.reNewPsw)
    EditText reNewPsw;

    ResetPasswordPresenter presenter;


    @Override
    public void setRootView() {
        setContentView(R.layout.act_change_psw);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new ResetPasswordPresenter(new ResetPasswordModel(), this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.save:
                if(!TextUtils.isEmpty(oldPsw.getText().toString()) && !TextUtils.isEmpty(newPsw.getText().toString()) &&
                        newPsw.getText().toString().equals(reNewPsw.getText().toString()))
                {
                    if(checkPwd(newPsw.getText().toString())>=2) {
                        if (newPsw.getText().toString().trim().length() > 7) {
                            Map<String, String> params = new HashMap<>();
                            params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                            params.put("newPassword", newPsw.getText().toString());
                            params.put("oldPassword", oldPsw.getText().toString());
                            params.put("accountId", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID));
                            presenter.resetPassword(params);
                        } else {
                            ViewInject.toast("输入字符位数不正确！");
                        }
                    }
                    else {
                        ViewInject.toast("请确认含数字、大写字母、小写字母、符号中两种或两种以上");
                    }
                }
                else {
                    ViewInject.toast("两次新密码输入不一致！");
                }
                break;
        }
    }

    @Override
    public void resetPassword(BaseEntity baseBean) {
        if(baseBean.getStatus().equals("200"))
        {
            PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.USERPWD, newPsw.getText().toString());
            ViewInject.toast("修改密码成功");
            finish();
        }
        else {
            ViewInject.toast(baseBean.getMessage());
        }
    }

    private int checkPwd(String pwd)
    {
        int i = pwd.matches(".*\\d+.*") ? 1 : 0;
        int j = pwd.matches(".*[a-z]+.*") ? 1 : 0;
        int k = pwd.matches(".*[A-Z]+.*") ? 1 : 0;
        int l = pwd.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*") ? 1 : 0;

        return  i+j+k+l;
    }
}
